package com.tripwego.client.ui.pageVoyage.top;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.tripwego.client.event.CloseActiviteEvent;
import com.tripwego.client.event.CloseActiviteEventHandler;
import com.tripwego.client.event.CloseStepEvent;
import com.tripwego.client.event.CloseStepEventHandler;
import com.tripwego.client.event.MajLblInfosEvent;
import com.tripwego.client.ui.pageVoyage.FormCreateActiviteWidget;
import com.tripwego.client.ui.pageVoyage.FormCreateEtapeWidget;
import com.tripwego.client.ui.pageVoyage.IVoyageView;

public class TopCenterWidget extends Composite implements IVoyageView, CloseStepEventHandler, CloseActiviteEventHandler {

	private Presenter presenter;

	private EventBus eventBus;

	private HorizontalPanel contentPanel = new HorizontalPanel();

	private HorizontalPanel horizontalPanel_actions = new HorizontalPanel();

	private Button btn_addEtape = new Button("Nouvelle Etape");

	private Button btn_addActivite = new Button("Nouvelle Activit\u00E9");

	private final HorizontalPanel horizontalPanel_form = new HorizontalPanel();

	private FormCreateEtapeWidget formCreateEtapeWidget = new FormCreateEtapeWidget();

	private FormCreateActiviteWidget formCreateActiviteWidget = new FormCreateActiviteWidget();

	public TopCenterWidget() {
		contentPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
		contentPanel.setWidth("");

		contentPanel.add(horizontalPanel_form);
		horizontalPanel_actions.setSpacing(5);

		contentPanel.add(horizontalPanel_actions);
		horizontalPanel_actions.setWidth("");

		horizontalPanel_actions.add(btn_addEtape);
		horizontalPanel_actions.setCellHorizontalAlignment(btn_addEtape, HasHorizontalAlignment.ALIGN_CENTER);

		horizontalPanel_actions.add(btn_addActivite);
		horizontalPanel_actions.setCellHorizontalAlignment(btn_addActivite, HasHorizontalAlignment.ALIGN_CENTER);

		horizontalPanel_form.add(formCreateEtapeWidget.asWidget());
		horizontalPanel_form.add(formCreateActiviteWidget.asWidget());

		btn_addActivite.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				horizontalPanel_actions.setVisible(false);
				formCreateEtapeWidget.hide();
				formCreateActiviteWidget.show();
				formCreateActiviteWidget.init();

				String message = "Nouvelle activite >> selectionnez une etape et cliquez sur un type d'activite.";
				eventBus.fireEvent(new MajLblInfosEvent(message));
			}
		});

		btn_addEtape.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				horizontalPanel_actions.setVisible(false);
				formCreateActiviteWidget.hide();
				formCreateEtapeWidget.show();
				formCreateEtapeWidget.init();

				String message = "Nouvelle etape >> localisez votre etape et selectionnez un mode de transport.";
				eventBus.fireEvent(new MajLblInfosEvent(message));
			}
		});
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.google.gwt.user.client.ui.Widget#asWidget()
	 */
	@Override
	public Widget asWidget() {
		return contentPanel;
	}

	@Override
	public void setPresenter(Presenter presenter) {
		this.presenter = presenter;

		formCreateEtapeWidget.setPresenter(presenter);
		formCreateActiviteWidget.setPresenter(presenter);
	}

	@Override
	public void setEventBus(EventBus eventBus) {
		this.eventBus = eventBus;

		formCreateEtapeWidget.setEventBus(eventBus);
		formCreateActiviteWidget.setEventBus(eventBus);

		// ecouteurs d'evenements
		eventBus.addHandler(CloseStepEvent.TYPE, this);
		eventBus.addHandler(CloseActiviteEvent.TYPE, this);
	}

	@Override
	public void init() {
		horizontalPanel_actions.setVisible(false);
		formCreateActiviteWidget.hide();
		
		formCreateEtapeWidget.show();
		formCreateEtapeWidget.init();
	}

	@Override
	public void handleCloseActiviteEvent(CloseActiviteEvent event) {
		horizontalPanel_actions.setVisible(true);
	}

	@Override
	public void handleCloseStepEvent(CloseStepEvent event) {
		horizontalPanel_actions.setVisible(true);
	}
}
